#include "myspinboxdelegate.h"

MySpinBoxDelegate::MySpinBoxDelegate()
{

}

QWidget *MySpinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QSpinBox *spinBox = new QSpinBox(parent);

    spinBox->setFrame(false);
    spinBox->setMinimum(0);
    spinBox->setMaximum(10000);

    return spinBox;
}

void MySpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    int v = index.model()->data(index, Qt::EditRole).toInt();
    QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
    spinBox->setValue(v);
}

void MySpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
    spinBox->interpretText();
    model->setData(index, spinBox->value(), Qt::EditRole);
}

void MySpinBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    editor->setGeometry(option.rect);
}
